package cn.tude.egweibo4.controller;


import cn.tude.egweibo4.base.response.JsonResult;
import cn.tude.egweibo4.base.response.StatusCode;
import cn.tude.egweibo4.mapper.CommentMapper;
import cn.tude.egweibo4.pojo.entity.Comment;
import cn.tude.egweibo4.pojo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("v1/comment/")
@Api(tags = "03.评论模块")
public class CommentController {
    @Autowired(required = false)
    private CommentMapper commentMapper;


    @PostMapping("insert")
    @ApiOperation(value = "发布评论")
    public JsonResult insertComment(@RequestBody  String content, Long weiboId,@ApiIgnore HttpSession session){
        Comment comment=new Comment();
        comment.setContent(content);
        comment.setWeiboId(weiboId);
        User user=(User) session.getAttribute("user");
        if (user==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        comment.setUserId(user.getId());
        int i = commentMapper.insertComment(comment);
        if (i==1)
            return JsonResult.ok();
        return JsonResult.notok();
    }

    @GetMapping("selectByWeiboId")
    @ApiOperation(value = "获取评论列表")
    @ApiImplicitParam(name = "id",value = "微博ID",required = true)
    public JsonResult selectByWeiboId(Long id) {
        return JsonResult.ok(commentMapper.selectByWeiboId(id));
    }
}
